/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * frmLogin.java
 *
 * Created on May 22, 2011, 4:28:06 AM
 */

package Login;

import Account.frmAccount;
import Admin.frmAdminCenter;
import Callcenter.frmCallCenter;
import Dispatch.frmDispatch;
import Ordermanagement.frmOrder;
import Technical.frmTechnical;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.KeyEvent;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author BachDEV
 */
public class frmLogin extends javax.swing.JFrame {
    Vector user = new Vector();
    public static int x;
    public static int y;

    /** Creates new form frmLogin */
    public frmLogin() {
        
        try{
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.windows.WindowsLookAndFeel");
        }catch(InstantiationException ex){
            ex.printStackTrace();
        }catch(UnsupportedLookAndFeelException ex){
            ex.printStackTrace();
        }catch(ClassNotFoundException ex){
            ex.printStackTrace();
        }catch(IllegalAccessException ex){
            ex.printStackTrace();
        }
        try {
            
            initComponents();
            Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
            setLocation((screen.width - this.getSize().width)/2,(screen.height - this.getSize().height)/2);
            this.setResizable(false);
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        }
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        txtUserName = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        pwdPassword = new javax.swing.JPasswordField();
        btnLogin = new javax.swing.JButton();
        btnReset = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Login");

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Login"));

        txtUserName.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtUserNameKeyPressed(evt);
            }
        });

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel1.setForeground(new java.awt.Color(0, 0, 153));
        jLabel1.setText("Username");

        jLabel2.setFont(new java.awt.Font("Tahoma", 1, 12));
        jLabel2.setForeground(new java.awt.Color(0, 0, 153));
        jLabel2.setText("Password");

        pwdPassword.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                pwdPasswordKeyPressed(evt);
            }
        });

        btnLogin.setText("Login");
        btnLogin.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnLoginActionPerformed(evt);
            }
        });

        btnReset.setText("Reset");
        btnReset.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnResetActionPerformed(evt);
            }
        });

        btnCancel.setText("Cancel");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(55, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1))
                .addGap(27, 27, 27)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtUserName, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE)
                    .addComponent(pwdPassword, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, 163, Short.MAX_VALUE))
                .addGap(69, 69, 69))
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(btnLogin, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnReset, javax.swing.GroupLayout.PREFERRED_SIZE, 99, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btnCancel, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(20, 20, 20)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtUserName, javax.swing.GroupLayout.PREFERRED_SIZE, 31, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(pwdPassword, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(18, 18, 18)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnLogin)
                    .addComponent(btnReset)
                    .addComponent(btnCancel))
                .addContainerGap(29, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 409, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 213, Short.MAX_VALUE)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtUserNameKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtUserNameKeyPressed
        // TODO add your handling code here:
        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            Connection connect = null;
            PreparedStatement stmt = null;
            ResultSet rs = null;
            String s = "The username or password is invalid!";
            String sql = "Select * from Users where userName = ?";
            try {
                connect = Connections.DBConnection.getConnection("eProject", "sa", "1234");
                stmt = connect.prepareStatement(sql);
                stmt.setString(1, txtUserName.getText());
                rs = stmt.executeQuery();

                if (rs.next()) {
                    if (pwdPassword.getText().equals(rs.getString("Password"))) {
                        int type = rs.getInt("Type");
                        switch (type) {
                             case 1:
                                frmAdminCenter fr = new frmAdminCenter(this);
                                fr.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 2:
                                frmCallCenter call = new frmCallCenter(this);
                                call.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 3:
                                frmDispatch dispatch = new frmDispatch(this);
                                dispatch.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 4:
                                frmOrder order = new frmOrder(this);
                                this.setVisible(false);
                                order.setVisible(true);
                                break;
                            case 5:
                                frmTechnical technical = new frmTechnical(this);
                                technical.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 6:
                                frmAccount account = new frmAccount(this);
                                account.setVisible(true);
                                this.setVisible(false);
                                break;
                            
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, s);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, s);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            } finally {
                try {
                    Connections.DBConnection.closeConnection(connect, stmt, rs);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                }
            }
        }
}//GEN-LAST:event_txtUserNameKeyPressed

    private void pwdPasswordKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_pwdPasswordKeyPressed

        if (evt.getKeyCode() == KeyEvent.VK_ENTER) {
            Connection connect = null;
            PreparedStatement stmt = null;
            ResultSet rs = null;
            String s = "The username or password is invalid!";
            String sql = "Select * from Users where userName = ?";
            try {
                connect = Connections.DBConnection.getConnection("eProject", "sa", "123");
                stmt = connect.prepareStatement(sql);
                stmt.setString(1, txtUserName.getText());
                rs = stmt.executeQuery();

                if (rs.next()) {
                    if (pwdPassword.getText().equals(rs.getString("Password"))) {
                        int type = rs.getInt("Type");
                        switch (type) {
                             case 1:
                                frmAdminCenter fr = new frmAdminCenter(this);
                                fr.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 2:
                                frmCallCenter call = new frmCallCenter(this);
                                call.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 3:
                                frmDispatch dispatch = new frmDispatch(this);
                                dispatch.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 4:
                                frmOrder order = new frmOrder(this);
                                this.setVisible(false);
                                order.setVisible(true);
                                break;
                            case 5:
                                frmTechnical technical = new frmTechnical(this);
                                technical.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 6:
                                frmAccount account = new frmAccount(this);
                                account.setVisible(true);
                                this.setVisible(false);
                                break;
                            
                        }
                    } else {
                        JOptionPane.showMessageDialog(this, s);
                    }
                } else {
                    JOptionPane.showMessageDialog(this, s);
                }
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            } finally {
                try {
                    Connections.DBConnection.closeConnection(connect, stmt, rs);
                } catch (Exception ex) {
                    JOptionPane.showMessageDialog(this, ex.getMessage());
                }
            }
        }
}//GEN-LAST:event_pwdPasswordKeyPressed

    private void btnLoginActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnLoginActionPerformed
        // TODO add your handling code here:
        Connection connect = null;
        PreparedStatement stmt = null;
        ResultSet rs = null;
        String s = "The username or password is invalid!";
        String sql = "Select * from Users where userName = ?";
        try {
            connect = Connections.DBConnection.getConnection("eProject", "sa", "123");
            stmt = connect.prepareStatement(sql);
            stmt.setString(1, txtUserName.getText());
            rs = stmt.executeQuery();

            if (rs.next()) {
                if (pwdPassword.getText().equals(rs.getString("Password"))) {
                    int type = rs.getInt("Type");                     
                    switch (type) {
                        case 1:
                                frmAdminCenter fr = new frmAdminCenter(this);
                                fr.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 2:
                                frmCallCenter call = new frmCallCenter(this);
                                call.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 3:
                                frmDispatch dispatch = new frmDispatch(this);
                                dispatch.setVisible(true);
                                this.setVisible(false);
                                break;
                            case 4:
                                frmOrder order = new frmOrder(this);
                                this.setVisible(false);
                                order.setVisible(true);
                                break;
                           case 5:
                                frmTechnical technical = new frmTechnical(this);
                                technical.setVisible(true);
                                this.setVisible(false);
                                break;
                           case 6:
                                frmAccount account = new frmAccount(this);
                                account.setVisible(true);
                                this.setVisible(false);
                                break;
                            
                    }
                } else {
                    JOptionPane.showMessageDialog(this, s);
                }
            } else {
                JOptionPane.showMessageDialog(this, s);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, ex.getMessage());
        } finally {
            try {
                Connections.DBConnection.closeConnection(connect, stmt, rs);
            } catch (Exception ex) {
                JOptionPane.showMessageDialog(this, ex.getMessage());
            }
        }
}//GEN-LAST:event_btnLoginActionPerformed

    private void btnResetActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnResetActionPerformed
        // TODO add your handling code here:
        txtUserName.setText("");
        pwdPassword.setText("");
}//GEN-LAST:event_btnResetActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        System.exit(1);
}//GEN-LAST:event_btnCancelActionPerformed

    /**
    * @param args the command line arguments
    */
   public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                
                new frmLogin().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnLogin;
    private javax.swing.JButton btnReset;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JPanel jPanel1;
    public javax.swing.JPasswordField pwdPassword;
    public javax.swing.JTextField txtUserName;
    // End of variables declaration//GEN-END:variables

}
